package com.nhh.v.service

import java.{util ⇒ jul}

import com.nhh.exception.ServiceException
import com.nhh.v.cons.ServerCons
import com.nhh.v.dto.HomeUserInfo
import com.nhh.v.model.NhhwakHashrateRanking
import com.nhh.v.utils.FormatterUtil
import com.nhh.v.utils.NhhUtil._
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
  * Created by ywp on 2018/9/6.
  */
@Service
class HomeService(@Autowired val usersService: UsersService,
                  @Autowired val coinsService: CoinsService,
                  @Autowired val hashRateService: HashRateService) {

  def getMainData(uid: String, pIndex: String, pSize: String): HomeMainData = {

    val user = usersService.getUserById(uid)
    Option(user) match {
      case None ⇒
        throw new ServiceException("3001", "用户信息获取失败")
      case _ ⇒
        val nhhMoney = FormatterUtil.format(BigDecimal(user.getNhhcoins) * ServerCons.nhhPrice)
        user.setNhhcoins(formatNHH(BigDecimal(user.getNhhcoins)))
        user.setNhhprice(ServerCons.nhhPrice)
        user.setNhhmoney(nhhMoney)
        user.setNhhmoneyDesc(s"(¥) ≈$nhhMoney")
        user.setHeadimgurl(getHeadImgUrl(user))
        val userCount = usersService.getUserCount()
        val hashRateRank = hashRateService.qryHashRateRank(uid.toInt, pIndex, pSize)
        val coins = coinsService.qrMyCoins(uid)
        val bonus = coinsService.qrMyCoins(uid, "townbonus")
        HomeMainData(user = user, hashrateRank = hashRateRank, userCount = userCount.toString, coins = coins, bonus = bonus, psize = pSize)
    }

  }

}

class ResultData(var retCode: String = "0", var retMsg: String = "成功")

case class HomeMainData(user: HomeUserInfo, hashrateRank: jul.List[NhhwakHashrateRanking],
                        userCount: String, coins: Seq[MyCoins], bonus: Seq[MyCoins], psize: String, growTip: String = "挖矿中..."
                       ) extends ResultData
